﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RussiaLibrary;
using System.Threading;
namespace Russia
{
    public partial class Tetris : Form
    {
        Paint pan;
        int interTime;
        Class_ControlArray _control = new Class_ControlArray();
        public Tetris()
        {
            InitializeComponent();
            Show();
            pan = new Paint(this.panelAll);
            pan.FillPanel(_control.Bytes_TetrisByte);//画界面
        }

        private void button_begin_Click(object sender, EventArgs e)
        {
            interTime = 200;
            Thread th = new Thread(new ParameterizedThreadStart(BeginGame));
            th.Start(interTime);
        }

        private void BeginGame(object interTime)
        {
            _control = new Class_ControlArray();
           while (true)
           {
                if(!_control.InitPolygon())
                {
                    return;
                }
                pan.FillPanel(_control.Bytes_TetrisByte);
                while (true)
                {
                    lock (Class_Common.TetrisLocker)
                    {
                        if (!_control.MoveDown())
                        {
                            break;
                        }
                        pan.FillPanel(_control.Bytes_TetrisByte);
                    }
                    Thread.Sleep((int)interTime);//调节下落速度。
                }
            }
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            lock(Class_Common.TetrisLocker)
            {
                switch(keyData)
                {
                    case Keys.Right:
                        _control.MoveRight();
                        break;
                    case Keys.Left:
                        _control.MoveLeft();
                        break;
                    case Keys.Down:
                        _control.MoveEnd();
                        break;
                    case Keys.Up:
                        break;
                }
              pan.FillPanel(_control.Bytes_TetrisByte);
            }
            return true;
        }

    }
}
